package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/ServletDemo1")
public class ServletDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的用户
        String username = req.getParameter("username");
        //2.获取HttpSession对象
        HttpSession session = req.getSession();
        //3.将用户设置到共享数据
        session.setAttribute("username",username);

        //实现url重写 相当于地址后面拼接了一个jsessionid
        PrintWriter pw = resp.getWriter();
        pw.write("<a href='"+resp.encodeURL("http://localhost:8080/session/ServletDemo3")+"'>go ServletDemo3</a>");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
